import { createApp } from "../main";

/**
 * 这里是服务器端的代码存在的位置，当应用启动会执行下面的逻辑
 */


export default (context: any) => {
  // Because it may be an asynchronous routing hook function or component, we will return a promise,
  // So that the server can wait for all the content before rendering,
  // It's ready.
  return new Promise((resolve, reject) => {
    const { app, router } = createApp();
    
    // Set the location of server router
    router.push(context.url);
    
    // Wait until the router parses the possible asynchronous components and hook functions
    router.onReady(() => {
      const matchedComponents = router.getMatchedComponents();
      
      // For unmatched routes, execute the reject function and return 404
      if (!matchedComponents.length) {
        return reject({ code: 404 });
      }
      
      // Promise should resolve the application instance so that it can render
      resolve(app);
    }, reject);
  });
};
